<?php

namespace SunAdmin\Common;

use SunAdmin\Service\DataDictionaryService;

class Utils
{
    /**
     * 构建树状结构数组
     */
    public static function buildTree($list, $pid = 0, $pidKey = 'pid'){
        $tree = [];
        foreach ($list as $item) {
            // 找到当前父节点的所有子节点
            if ($item[$pidKey] == $pid) {
                // 递归查找子节点的子节点
                $children = self::buildTree($list, $item['id'],$pidKey);
                if ($children) {
                    $item['children'] = $children;
                }
                $tree[] = $item;
            }
        }
        return $tree;
    }

    /**
     * 获取配置参数,优先缓存
     * 封装目的是嫌DataDictionaryService类名字太长
     */
    public static function getData(array|string $keys, $fromDb = false){
        return DataDictionaryService::getValue($keys, $fromDb);
    }
	
	/**
	 * 时间戳转ymdhis
	 */
	public static function timeToStr($time = null){
		if (!$time) {
			$time = time();
		}
		return date('Y-m-d H:i:s', $time);
	}
}